package chixing.day13.class32;

/**
 * ------------值传递
 *
 * int a = 10;              (基本数据类型 ： 栈)
 * int value[返回值] =a;     (将a 的值 复制copy 一份给 b , b = 10)
 * a = 20;                  (a 发生了改变，b 不受影响)
 *
 * -------------地址传递
 * Book book = new Book("Java",100);
 * Book value[返回值] = book;
 *  book.setBookName("Python");
 *  book.setBookPrice(77);
 */
public class FinallyReturnDemo {
    public static void main(String[] args) {
        int result = fun1(0);
        System.out.println("方法的返回值是：" + result); // 10

        Book book = fun2();
        System.out.println("方法的返回值是：" + book);
    }
    /**
     *try- catch - finally
     * 1.  try-catch-finally 中有return ， 先执行return ， 在执行finally
     2. 在finally中修改了 返回值变量， 返回值会不会改变的问题？？？？
     a. 返回值类型 是基本数据类型，本质执行的是 值传递，返回值不会改变 . 参考  fun1()
     b.返回值类型 是引用数据类型，，本质执行的是 地址传递，返回值会改变 . 参考 fun2()
     返回值根据类型来决定，返回值变量会改变
     */
    public static int fun1(int b){
        int a = 10;
        try{
            System.out.println("----try------");
            return  a;  // int value[返回值] = a 值传递， 将a 的值 copy 给value
        }catch (Exception e){
            System.out.println("----catch------");
            return --a; // return 9;
        }finally {
            a+=2;
            System.out.println("finally---------a = " + a); // a = 12
        }
    }


    public static Book fun2(){
        Book book = new Book("Java",100);
        try{
            System.out.println("-----try--------");
            return book;  // Book value(返回值) = book;  即 book 、value 指向同一块堆内存
        }catch (Exception e){
            return null;
        }finally {
            System.out.println("-----finally--------");
            book.setBookName("Python");
            book.setBookPrice(77);
        }
    }
}

class Book{
    String bookName;
    float bookPrice;

    public Book(String bookName, float bookPrice) {
        this.bookName = bookName;
        this.bookPrice = bookPrice;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public float getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(float bookPrice) {
        this.bookPrice = bookPrice;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", bookPrice=" + bookPrice +
                '}';
    }

}
